<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/9 0009
 * Time: 14:30
 */
namespace app\admin\controller;
use app\admin\model\AdminUser;
use app\admin\model\Tables;
use app\admin\model\UpFile;
use think\Db;
use think\Request;

class Add
{
    /**添加课程分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function classes(Request $request){
        if(!session('adminuser')){
            return json(['code'=>401,'msg'=>'请登录']);
        }
        $class_name = $request->param('class_name',null);
        if(!$class_name){
            return json(['code'=>205,'msg'=>'请输入你要添加的分类名称']);
        }
        if(Db::name('classes')->insert(['class_name'=>$class_name,'create_time'=>time()])){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>203,'msg'=>'添加失败']);
        }
    }

    /**添加分支
     * @param Request $request
     * @return \think\response\Json
     */
    public function branch(Request $request){
        $classid = $request->param('classid',null);
        if(!$classid){
            return json(['code'=>205,'msg'=>'请输入你要添加的分类名称']);
        }
        $branch_name = $request->param('branch_name',null);
        if(!$branch_name){
            return json(['code'=>205,'msg'=>'请输入你要添加的分支名称']);
        }
        if(Db::name('branch')->insertGetId(['branch_name'=>$branch_name,'tables_class'=>$classid])){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>205,'msg'=>'添加失败']);
    }
    }

    /**添加题目
     * @param Request $request
     * @return \think\response\Json
     */
    public function add_bank(Request $request){
        $data = $request->param();
        $val  = val($data,[
            'classes_id|课程分类'=>'require',
            'branch_id|课程分支'=>'require',
            'three_level_id|课程分级'   =>'require',
            'stem|题干'        =>'require',
            'answer|答案'      =>'require',
            'right_key|正确答案'=>'require|/[ABCDEFG]/',
            'coin|获取的金币'   =>'require',
            'experience|获取的经验'   =>'require',
            'difficulty|难度星等'    =>'require',
            'type|题目模式'    =>'require',
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        $answer = [];
        $answer1 = explode('；',$data['answer']);
        foreach ($answer1 as $value){
            $answer2 = explode(';',$value);
            foreach ($answer2 as $values){
                $answer = array_merge($answer,explode('。',$values));
            }
        }
        $data['answer'] = [];
        foreach ($answer as $value){
            if($value){
                $data['answer'][] = $value;
            }
        }
        if($data['right_key']=='A'){
            $data['right_key']=0;
        }elseif ($data['right_key']=='B'){
            $data['right_key']=1;
        } elseif ($data['right_key']=='C'){
            $data['right_key']=2;
        } elseif ($data['right_key']=='D'){
            $data['right_key']=3;
        }elseif ($data['right_key']=='E'){
            $data['right_key']=4;
        }elseif ($data['right_key']=='F'){
            $data['right_key']=5;
        }elseif ($data['right_key']=='G'){
            $data['right_key']=6;
        }
        $data['create_time'] = time();
        if(UpFile::isFile('picture')){
            $fileName = $request->file('picture');
            $fileName = UpFile::upload($fileName);
            if($fileName){
                $data['picture'] = $fileName;
            }
        }

        $data['status'] = 1;
        if(Db::name('bank')->json(['answer'])->insert($data)){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>203,'msg'=>'添加失败']);
        }
    }

    /**添加职业名称
     * @param Request $request
     * @return \think\response\Json
     */
    public function occupation(Request $request){
        $data = $request->param();
        $val  = val($data,[
            'pid|所属职业'=>'require',
            'occupation_name|职业名称'=>'require',
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        if(Db::name('occupation')->insert($data)){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>200,'msg'=>'添加失败']);
        }
    }

    /**添加等级
     * @param Request $request
     * @return \think\response\Json
     */
    public function grade(Request $request){
        $data = $request->param();
        $val = val($data,[
            'number|等级名称'=>'require',
            'grade_experience|所需经验'=>'require'
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        if(Db::name('grade')->insert($data)){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>203,'msg'=>'添加失败']);
        }
    }

    /**添加vip
     * @param Request $request
     * @return \think\response\Json
     */
    public function vip_price(Request $request){
        $data = $request->param();
        $val = val($data,[
            'vip_name|vip名称'=>'require',
            'vip_icon|vip等级'=>'require',
            'vip_price|vip价格'=>'require',
            'vip_time|vip时长'=>'require',
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        $file = $request->file('vip_picture');
        $fileName = UpFile::upload($file);
        if($fileName){
            $data['vip_picture'] = $fileName;
        }
        if(Db::name('vip_price')->insert($data)){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>203,'msg'=>'添加失败']);
        }
    }

    /**添加第三级分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function three_level(Request $request){
        $data = $request->param();
        $val = val($data,[
            'classes_id|第一级分类'=>'require',
            'branchid|第二级分类'=>'require',
            'three_level_name|第三级分类名称'=>'require',
            'recommend|是否推荐'=>'require'
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        $data['spot'] = 0;
        if(Db::name('three_level')->insert($data)){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>203,'msg'=>'添加失败']);
        }
    }

    /**添加题目模式
     * @param Request $request
     * @return \think\response\Json
     */
    public function situation(Request $request){
        $data = $request->param();
        if(!$data['situation_name']){
            return json(['code'=>203,'msg'=>'添加模式名称']);
        }
        if(Db::name('situation')->insert($data)){
            return json(['code'=>200,'msg'=>'添加成功']);
        } else{
            return json(['code'=>203,'msg'=>'添加失败']);
        }
    }

    /**添加配置项
     * @param Request $request
     * @return \think\response\Json
     */
    public function system(Request $request){
        $data = $request->param();
        $val  = val($data,[
            'system_name|设置名称'=>'require',
            'identification|设置标识'=>'require|/[qwertyuiopasdfghjklzxcvbnm]/',
            'system_value|设置值'=>'require',
            'explain|说明'=>'require'
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        if(Db::name('system')->where('identification',$data['identification'])->find()){
            return json(['code'=>203,'msg'=>'这个标识已经存在']);
        }
        if(Db::name('system')->insert($data)){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>200,'msg'=>'添加失败']);
        }
    }

    /**添加后台权限
     * @param Request $request
     * @return \think\response\Json
     */
    public function admin_power(Request $request){
        $data =$request->param();
        if(!AdminUser::Auth('all')){
            return '你没有权限';
        }
        $val = val($data,[
            'power_name|权限名称'=>'require',
            'identification|权限标识'=>'require|/[qwertyuiopasdfghjklzxcvbnm]/'
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        if(Db::name('admin_power')->insert($data)){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>200,'msg'=>'添加失败']);
        }
    }

    /**添加后台用户
     * @param Request $request
     * @return string|\think\response\Json
     */
    public function admin_user(Request $request){
        $data =$request->param();
        if(!AdminUser::Auth('all')){
            return '你没有权限';
        }
        $val = val($data,[
            'admin_user|后台用户名'=>'require',
            'admin_pwd|后台密码'=>'require',
            'admin_power|后台权限'=>'require',
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        $data['admin_pwd'] = AdminUser::getPwd($data['admin_pwd']);
        if(UpFile::isFile('admin_portrait')){
            $file = $request->file('admin_portrait');
            $fileName = UpFile::upload($file);
            if($fileName){
                $data['admin_portrait'] = $fileName;
            }
        }
        if(Db::name('admin_user')->insert($data)){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>200,'msg'=>'添加失败']);
        }
    }
}